﻿using Abp.Application.Services.Dto;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Akso.Entities;
using Akso.Outbounds.Dto;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic;
using Abp.Linq.Extensions;
using Abp.AutoMapper;
using Abp.Authorization;
using Akso.Authorization;

namespace Akso.Outbounds
{
    [AbpAuthorize(AppPermissions.Pages_Inventory_Outbounds)]
    public class OutboundAppService: AbpZeroTemplateAppServiceBase,IOutboundAppService
    {
        private readonly IRepository<Outbound, Guid> _outboundsRepository;

        public OutboundAppService(IRepository<Outbound, Guid> outboundsRepository)
        {
            _outboundsRepository = outboundsRepository;
        }
        /// <summary>
        /// 这个方法如果没有写，界面上的查询一直在打转
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultOutput<OutboundListDto>> GetObjects(GetOutboundsInput input)
        {
            var query = getQuery(input);
            var dataCount = await query.CountAsync();
            var data = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();

            return new PagedResultOutput<OutboundListDto>(
                dataCount,
                data.MapTo<List<OutboundListDto>>()
                );
        }
        private IQueryable<Outbound> getQuery(GetOutboundsInput input)
        {
            var query = _outboundsRepository.GetAll()
                .WhereIf(
                    !input.Filter.IsNullOrWhiteSpace(),
                    t =>
                        t.Vendor.Contains(input.Filter) ||
                        t.MaterialNameCn.Contains(input.Filter)
                );
            return query;
        }
    }
}
